from abc import ABC
import snap7
from utilsnap7 import *
from snap7.types import areas
import os
from PyQt5.QtCore import pyqtSlot, QObject
from PyQt5.QtWidgets import QMessageBox


# from tkinter import messagebox


def Writeoutput(dev, byte, bit, cmd):
    data = dev.read_area(snap7.types.Areas.PA, 0, byte, 1)
    snap7.util.set_bool(data, 0, bit, cmd)
    dev.write_area(snap7.types.Areas.PA, 0, byte, data)


def Readoutput(dev, byte, bit):
    data = dev.read_area(snap7.types.Areas.PA, 0, byte, 1)
    status = get_bool(data, 0, bit)
    return status


rack = 0
slot = 1
plc_model = 3
q_area_byte = 3
q_area_bit = 8


class SMU3003(QObject):
    def __init__(self, ip, parent=None):
        super(SMU3003, self).__init__(parent)
        self._ip = ip
        self.siemens_plc = snap7.client.Client()
        self.siemens_plc.set_connection_type(plc_model)
        self.state = None
        self.rack = 0
        self.slot = 1
        


    def smu3003_init(self):
        self.siemens_plc.connect(self._ip, rack=0, slot=1)
        return self.siemens_plc.get_connected()

    def connect(self):
        try:
            # 该方法的返回值为整数，当成功建立连接时返回0；如果返回值为非零整数则代表某种错误
            num = self.siemens_plc.connect(self._ip, rack=self.rack, slot=self.slot)
        except Exception as e:
            print("Switch Box connection failed")
            return False
        return True if not num else False

    def disconnect(self):
        self.siemens_plc.disconnect()
        self.state = None

    # def init(self) -> bool:
    #     print('Starting smu3003...')
    #     if self.smu3003_init():
    #         print('connect to smu3003 success')
    #         self.smu3003_preset()
    #         self.disconnect()
    #         return True
    #     else:
    #         print('connect to smu3003 failure')
    #         return False

    def front_prsest(self):
        # 前8重置
        Writeoutput(self.siemens_plc, 0, 0, False)
        Writeoutput(self.siemens_plc, 0, 1, False)
        Writeoutput(self.siemens_plc, 0, 2, False)
        Writeoutput(self.siemens_plc, 0, 3, False)
        Writeoutput(self.siemens_plc, 0, 4, False)
        Writeoutput(self.siemens_plc, 0, 5, False)
        Writeoutput(self.siemens_plc, 0, 6, False)
        Writeoutput(self.siemens_plc, 0, 7, False)

    def switch_preset(self):
        # 滤波器重置
        Writeoutput(self.siemens_plc, 1, 0, False)
        Writeoutput(self.siemens_plc, 1, 1, False)
        Writeoutput(self.siemens_plc, 1, 2, False)
        Writeoutput(self.siemens_plc, 1, 3, False)
        Writeoutput(self.siemens_plc, 1, 4, False)
        Writeoutput(self.siemens_plc, 1, 5, False)
        Writeoutput(self.siemens_plc, 1, 6, False)
        Writeoutput(self.siemens_plc, 1, 7, False)
        Writeoutput(self.siemens_plc, 2, 0, False)
        Writeoutput(self.siemens_plc, 2, 1, False)
        Writeoutput(self.siemens_plc, 2, 2, False)
        Writeoutput(self.siemens_plc, 2, 3, False)
        Writeoutput(self.siemens_plc, 2, 4, False)
        Writeoutput(self.siemens_plc, 2, 5, False)

    def amp_preset(self):
        # 是否接入放大器
        Writeoutput(self.siemens_plc, 2, 6, False)
        Writeoutput(self.siemens_plc, 2, 7, False)




    def switch_to_LTE1(self):
        if self.smu3003_init():
            self.front_prsest()
            Writeoutput(self.siemens_plc, 0, 0, True)
            self.disconnect()
    def switch_to_LTE2(self):
        if self.smu3003_init():
            self.front_prsest()
            Writeoutput(self.siemens_plc, 0, 1, True)
            self.disconnect()
    def switch_to_LTE3(self):
        if self.smu3003_init():
            self.front_prsest()
            Writeoutput(self.siemens_plc, 0, 2, True)
            self.disconnect()
    def switch_to_LTE4(self):
        if self.smu3003_init():
            self.front_prsest()
            Writeoutput(self.siemens_plc, 0, 3, True)
            self.disconnect()
    def switch_to_NB1(self):
        if self.smu3003_init():
            self.front_prsest()
            Writeoutput(self.siemens_plc, 0, 4, True)
            self.disconnect()
    def switch_to_NB2(self):
        if self.smu3003_init():
            self.front_prsest()
            Writeoutput(self.siemens_plc, 0, 5, True)
            self.disconnect()
    def switch_to_NB3(self):
        if self.smu3003_init():
            self.front_prsest()
            Writeoutput(self.siemens_plc, 0, 6, True)
            self.disconnect()
    def switch_to_NB4(self):
        if self.smu3003_init():
            self.front_prsest()
            Writeoutput(self.siemens_plc, 0, 7, True)
            self.disconnect()

    def switch_to_824_894(self):
        if self.smu3003_init():
            self.switch_preset()
            Writeoutput(self.siemens_plc, 1, 0, True)
            Writeoutput(self.siemens_plc, 1, 2, True)
            self.disconnect()
    def switch_to_880_960(self):
        if self.smu3003_init():
            self.switch_preset()
            Writeoutput(self.siemens_plc, 1, 0, True)
            Writeoutput(self.siemens_plc, 1, 3, True)
            self.disconnect()
    def switch_to_1710_1880(self):
        if self.smu3003_init():
            self.switch_preset()
            Writeoutput(self.siemens_plc, 1, 0, True)
            Writeoutput(self.siemens_plc, 1, 4, True)
            self.disconnect()
    def switch_to_1880_1920(self):
        if self.smu3003_init():
            self.switch_preset()
            Writeoutput(self.siemens_plc, 1, 0, True)
            Writeoutput(self.siemens_plc, 1, 5, True)
            self.disconnect()
    def switch_to_1920_2170(self):
        if self.smu3003_init():
            self.switch_preset()
            Writeoutput(self.siemens_plc, 1, 0, True)
            Writeoutput(self.siemens_plc, 1, 6, True)
            self.disconnect()
    def switch_to_2300_2400(self):
        if self.smu3003_init():
            self.switch_preset()
            Writeoutput(self.siemens_plc, 1, 0, True)
            Writeoutput(self.siemens_plc, 1, 7, True)
            self.disconnect()
    def switch_to_2580_2620(self):
        if self.smu3003_init():
            self.switch_preset()
            Writeoutput(self.siemens_plc, 1, 0, False)
            Writeoutput(self.siemens_plc, 2, 0, True)
            self.disconnect()
    def switch_to_2496_2690(self):
        if self.smu3003_init():
            self.switch_preset()
            Writeoutput(self.siemens_plc, 1, 0, False)
            Writeoutput(self.siemens_plc, 2, 1, True)
            self.disconnect()
    def switch_to_stright(self):
        if self.smu3003_init():
            self.switch_preset()
            Writeoutput(self.siemens_plc, 1, 0, False)
            Writeoutput(self.siemens_plc, 2, 2, True)
            self.disconnect()
    def switch_to_open(self):
        if self.smu3003_init():
            self.switch_preset()
            Writeoutput(self.siemens_plc, 1, 0, False)
            Writeoutput(self.siemens_plc, 2, 3, True)
            self.disconnect()
    def switch_to_amp(self):
        if self.smu3003_init():
            self.amp_preset()
            Writeoutput(self.siemens_plc, 2, 6, True)
            self.disconnect()

    def switch_to_no_amp(self):
        if self.smu3003_init():
            self.amp_preset()
            Writeoutput(self.siemens_plc, 2, 6, False)
            self.disconnect()

